package de.grosjohann.android.feuerwehr.domain;

import java.io.Serializable;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import de.grosjohann.android.feuerwehr.type.WeatherWarningType;

public class WeatherWarning
  implements Serializable {

  private final WeatherWarningType level;
  private final String msg;
  private final int color;

  public WeatherWarning(JSONObject obj) throws JSONException {
    color = Color.parseColor(obj.getString("color"));
    msg = obj.getString("msg");
    level = WeatherWarningType.valueOf(obj.getInt("level"));
  }

  public WeatherWarningType getLevel() {
    return level;
  }

  public String getMessage() {
    return msg;
  }

  public int getColor() {
    return color;
  }

  @Override
  public String toString() {
    return level.name() + ": " + msg;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((level == null) ? 0 : level.hashCode());
    result = prime * result + ((msg == null) ? 0 : msg.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    WeatherWarning other = (WeatherWarning) obj;
    if (level != other.level)
      return false;
    if (msg == null) {
      if (other.msg != null)
        return false;
    } else if (!msg.equals(other.msg))
      return false;
    return true;
  }

  public static WeatherWarning getHighestWarning(List<WeatherWarning> list) {
    WeatherWarning highestWarning = null;
    for (WeatherWarning warning : list) {
      if (highestWarning == null || warning.getLevel().getLevel() > highestWarning.getLevel().getLevel()) {
        highestWarning = warning;
      }
    }
    return highestWarning;
  }

}
